home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter_lib / Library / samples / landscape.c < prev    next >
C/C++ Source or Header  |  1999-12-03  |  5KB  |  154 lines

  1. /*
  2. **      $VER: landscape.c 1.00 (27.3.1999)
  3. **
  4. **      Creation date     : 27.3.1999
  5. **
  6. **      Description       :
  7. **         MeshWriter testprogram shape module.
  8. **
  9. **
  10. **      Written by Stephan Bielmann
  11. **
  12. */
  13.  
  14. /*************************** Includes *******************************/
  15.  
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. #include <meshwriter/meshwriter.h>
  20. #include <pragma/meshwriter_lib.h>
  21.  
  22. /**************************** Defines *******************************/
  23.  
  24. #define GRID_SIZE     32
  25.  
  26. /*********************** Type definitions ***************************/
  27.  
  28. /********************** Private functions ***************************/
  29.  
  30. static TOCLFloat randomHeight (FLOAT range)
  31. {
  32.   return (TOCLFloat) (range * ((TOCLFloat) (2.0 * rand () / RAND_MAX) - 1.0));
  33. }
  34.  
  35. /********************** Public functions ****************************/
  36.  
  37. /********************************************************************\
  38. *                                                                    *
  39. * Name         : landscape                                           *
  40. *                                                                    *
  41. * Description  : Draws a landscape.                                  *
  42. *                                                                    *
  43. * Arguments    : mesh    IN  : An initialized mesh handle.           *
  44. *                                                                    *
  45. * Comment      : This function is a "remake" of the Amiga-Magzin its *
  46. *                landscape sample code used in theyr Cyber-GL        *
  47. *                workshop.                                           *
  48. *                                                                    *
  49. \********************************************************************/
  50. void landscape(ULONG mesh) {
  51.   TOCLVertex grid[GRID_SIZE+1][GRID_SIZE+1];
  52.   TOCLColor color;
  53.   ULONG c[3];
  54.   int x, z;       /* x/z index */
  55.   int gridStep;   /* actual grid index distance */
  56.   int step;       /* actual generation step */
  57.   int seed,steps,i,j;
  58.   float maxHeight,minh,maxh;
  59.   int xl,xr,zl,zr;
  60.  
  61.   seed=rand();
  62.   steps=5;
  63.   maxHeight=5;
  64.  
  65.   MWLMeshMaterialAdd(mesh,&c[0]);
  66.   color.r=0,color.g=0,color.b=255;
  67.   MWLMeshMaterialDiffuseColorSet(mesh,c[0],&color);
  68.  
  69.   MWLMeshMaterialAdd(mesh,&c[1]);
  70.   color.r=0,color.g=255,color.b=0;
  71.   MWLMeshMaterialDiffuseColorSet(mesh,c[1],&color);
  72.  
  73.   MWLMeshMaterialAdd(mesh,&c[2]);
  74.   color.r=255,color.g=255,color.b=255;
  75.   MWLMeshMaterialDiffuseColorSet(mesh,c[2],&color);
  76.  
  77.   for(i=0;i<(GRID_SIZE+1);i++) {
  78.     for(j=0;j<(GRID_SIZE+1);j++) {
  79.       grid[i][j].x=i;
  80.       grid[i][j].y=j;
  81.       grid[i][j].z=0;
  82.     }
  83.   }
  84.  
  85.   srand (seed);
  86.   grid [        0][        0].z = randomHeight (maxHeight);
  87.   grid [GRID_SIZE][        0].z = randomHeight (maxHeight);
  88.   grid [        0][GRID_SIZE].z = randomHeight (maxHeight);
  89.   grid [GRID_SIZE][GRID_SIZE].z = randomHeight (maxHeight);
  90.  
  91.   gridStep = GRID_SIZE / 2;
  92.  
  93.   for (step = 1; step <= steps; step++) {
  94.     maxHeight /= 2.0;
  95.     for (x = gridStep / 2; x < GRID_SIZE; x += gridStep) {
  96.       xl = x - gridStep / 2;
  97.       xr = x + gridStep / 2;
  98.  
  99.       for (z = gridStep / 2; z < GRID_SIZE; z += gridStep) {
  100.         zl = z - gridStep / 2;
  101.         zr = z + gridStep / 2;
  102.  
  103.         grid [x][z].z = (grid [xl][zl].z +
  104.                          grid [xl][zr].z +
  105.                          grid [xr][zl].z +
  106.                          grid [xr][zr].z) / 4.0 + randomHeight (maxHeight);
  107.         grid [xl][z].z = (grid [xl][zl].z +
  108.                           grid [xl][zr].z) / 2.0 + randomHeight (maxHeight);
  109.         grid [x][zl].z = (grid [xl][zl].z +
  110.                           grid [xr][zl].z) / 2.0 + randomHeight (maxHeight);
  111.       }
  112.       grid [x][GRID_SIZE].z = (grid [xl][GRID_SIZE].z +
  113.                                grid [xr][GRID_SIZE].z) / 2.0 + randomHeight (maxHeight);
  114.  
  115.     }
  116.     for (z = gridStep / 2; z < GRID_SIZE; z += gridStep) {
  117.       grid [GRID_SIZE][z].z = (grid [GRID_SIZE][z - gridStep / 2].z +
  118.                                grid [GRID_SIZE][z + gridStep / 2].z) / 2.0 + randomHeight (maxHeight);
  119.     }
  120.     gridStep /= 2;
  121.   }
  122.  
  123.   maxh=-1000;minh=1000;
  124.   for(i=0;i<(GRID_SIZE);i++) {
  125.     for(j=0;j<(GRID_SIZE);j++) {
  126.                 if(maxh<grid[i][j].z) maxh=grid[i][j].z;
  127.                 if(minh>grid[i][i].z) minh=grid[i][j].z;
  128.     }
  129.   }
  130.  
  131.   for(i=0;i<(GRID_SIZE);i++) {
  132.     for(j=0;j<(GRID_SIZE);j++) {
  133.                 ULONG col;
  134.                 
  135.                 if(minh<0) {
  136.                         col=ULONG(((grid[i][j].z-minh))/((maxh-minh)/3));
  137.                 } else {
  138.                         col=ULONG(((grid[i][j].z+minh))/((maxh-minh)/3));
  139.                 }
  140.                 if(col>2)col=2;if(col<0)col=0;
  141.  
  142.                 MWLMeshPolygonAdd(mesh,c[col]);
  143.                 MWLMeshPolygonVertexAdd(mesh,&grid[i][j]);
  144.                 MWLMeshPolygonVertexAdd(mesh,&grid[i+1][j]);
  145.                 MWLMeshPolygonVertexAdd(mesh,&grid[i+1][j+1]);
  146.                 MWLMeshPolygonVertexAdd(mesh,&grid[i][j+1]);
  147.     }
  148.   }
  149.  
  150.   MWLMeshNameSet(mesh,"Landscape");
  151. }
  152.  
  153. /************************* End of file ******************************/
  154.